/*
 * @Description:导出
 * @Author: yech
 * @Date: 2021-09-07
 */
import { Ref } from 'vue';
import type { StoreData } from '@/types';

function isMSbrowser() {
    const userAgent = window.navigator.userAgent;
    return userAgent.indexOf('Edge') !== -1 || userAgent.indexOf('Trident') !== -1;
}

export function useExport(monitorData: Ref<StoreData>): {
    exportData: () => void;
} {
    function exportData() {
        const reTitle = 'user.json';
        const dataStr = monitorData.value ? JSON.stringify(monitorData.value) : '';

        if (isMSbrowser()) {
            const blob = new Blob([dataStr], { type: 'text/plain;charset=utf-8' });
            (window.navigator as any).msSaveBlob(blob, reTitle);
        } else {
            const a = document.createElement('a');
            a.href = 'data:text/json;charset=utf-8,' + dataStr;
            a.download = reTitle;
            a.click();
        }
    }

    return {
        exportData
    };
}
